package com.dp.appi.dao.impl;

import com.dp.appi.dao.MaterialDao;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2016/5/12.
 * 材料
 */
@Component
public class MaterialDaoImpl extends BaseDaoImpl implements MaterialDao {

    /**
     * 材料列表接口
     *
     * @param name 材料名称
     * @param s    开始序列
     * @param n    获取条数
     * @return
     */
    @Override
    public List<Map<String, Object>> materialList(String type,String name,String versionid, Integer s, Integer n) {

        String sql="select * from c_material where is_wire=0 ";
        if(!StringUtils.isEmpty(name)){
            sql+=" and name like '%"+name+"%'";
        }
        if(! versionid.equals("0"))
        {
            sql+=" and codeset_version_id = '"+versionid+"'";
        }
        if(!StringUtils.isEmpty(type) && !type.equals("全部")){
            sql+=" and type like '%"+type+"%'";
        }
        sql+=" order by sort,material_id ";
        sql+=" asc limit ?,?";
        return getJdbcTemplate().query(sql, new Object[]{s,n},getColumnMapRowMapper());
    }

    /**
     * 材料列表接口
     *
     * @param name 材料名称
     * @return
     */
    @Override
    public List<Map<String, Object>> materialListAll(String type,String name,String versionid) {
        String sql="select * from c_material where is_wire=0 ";
        if(!StringUtils.isEmpty(name)){
            sql+=" and name like '%"+name+"%'";
        }
        if(! versionid.equals("0"))
        {
            sql+=" and codeset_version_id = '"+versionid+"'";
        }
        if(!StringUtils.isEmpty(type) && !type.equals("全部")){
            sql+=" and type like '%"+type+"%'";
        }
        sql+=" order by sort,material_id ";
        return getJdbcTemplate().query(sql,getColumnMapRowMapper());
    }

    @Override
    public List<String> materialType(String versionid) {
        String sql="select distinct c_material.type from c_material where is_wire=0 ";
        if(! versionid.equals("0"))
        {
            sql+=" and codeset_version_id = '"+versionid+"'";
        }
        sql+=" order by sort asc";
        List<String> strings=new ArrayList<>();
        List<Map<String, Object>> list=getJdbcTemplate().query(sql,getColumnMapRowMapper());
        for(Map<String, Object> map:list){
            strings.add((String) map.get("type"));
        }
        return strings;
    }
}
